/*
 * @Author: your name
 * @Date: 2021-01-04 09:50:30
 * @LastEditTime: 2021-01-25 23:03:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \yellow-member-admin\src\router\index.js
 */
import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const router = new Router({
    routes: [{
            path: '/',
            redirect: '/dashboard'
        },
        {
            path: '/',
            component: () =>
                import ( /* webpackChunkName: "home" */ '../layout/Home.vue'),
            meta: { title: '自述文件' },
            children: [{
                    path: '/dashboard',
                    component: () =>
                        import ( /* webpackChunkName: "dashboard" */ '@/page/home'),
                    meta: { title: '系统首页' }
                },
                {
                    path: '/qr-code-list',
                    component: () =>
                        import ( /* webpackChunkName: "dashboard" */ '@/page/qr-code-list'),
                    meta: { title: '销售列表' }
                },
                {
                    path: '/qr-code',
                    component: () =>
                        import ( /* webpackChunkName: "dashboard" */ '@/page/qr-code'),
                    meta: { title: '添加销售' }
                },
                {
                    path: '/page-list',
                    component: () =>
                        import ( /* webpackChunkName: "dashboard" */ '@/page/page-list'),
                    meta: { title: '页面列表' }
                },
                {
                    path: '/add-page',
                    component: () =>
                        import ( /* webpackChunkName: "dashboard" */ '@/page/page-add'),
                    meta: { title: '添加页面' }
                },
                {
                    path: '/page-img-add',
                    component: () =>
                        import ( /* webpackChunkName: "dashboard" */ '@/page/page-img-add'),
                    meta: { title: '页面图片置入' }
                },
                {
                    path: '/page-img-list',
                    component: () =>
                        import ( /* webpackChunkName: "dashboard" */ '@/page/page-img-list'),
                    meta: { title: '页面图片列表' }
                },
                {
                    path: '/404',
                    component: () =>
                        import ( /* webpackChunkName: "404" */ '@/page/404.vue'),
                    meta: { title: '404' }
                },
                {
                    path: '/403',
                    component: () =>
                        import ( /* webpackChunkName: "403" */ '@/page/403.vue'),
                    meta: { title: '403' }
                }
            ]
        },
        {
            path: '/login',
            component: () =>
                import ( /* webpackChunkName: "login" */ '@/page/login'),
            meta: { title: '登录' }
        },
        {
            path: '*',
            redirect: '/404'
        }
    ]
});
// 导航守卫
// 判断用户是否登录，添加前置导航守卫
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} `;
    const token = localStorage.getItem('token');
    if (token) {
        next()
    } else {
        if (to.path === '/login') { // 如果是登录页
            next();
        } else {
            next({ path: '/login' })
        }
    }
})  
export default router